package com.bioscript.standalone.imps;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.bioscript.standalone.UtilMethods;
import com.bioscript.standalone.IO.FileReader;
import com.bioscript.standalone.dataStructures.Range;

public class SNPProcessor {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String snpFile = args[0];
		String outSNPFile = args[1];
		String type = args[2];
		List<Range> ranges = null;
		if (type.equals("DeNovo"))
		{
			ranges  = FileReader.readMummerSNPs(snpFile);
			filterMummerSnps(ranges);
		}
		else if (type.equals("Mapped"))
		{
			ranges  = FileReader.read454SNPs(snpFile);
			filter454Snps(ranges);
		}
		UtilMethods.WriteToFile(ranges, outSNPFile, 4);
	}
	
	private static void filter454Snps(List<Range> ranges) {
		Iterator<Range> itr = ranges.iterator();
		while(itr.hasNext())
		{
			Range r = itr.next();
			if (r.getLowerLimit() != r.getUpperLimit())
			{
				itr.remove();
			}
			else
			{
				String[] params = r.getMetadata().trim().split("\t");
				String coverage = params[params.length - 1];
				if (Integer.parseInt(coverage.substring(0,coverage.length() - 1)) < 50)
				{
					itr.remove();
				}
			}
		}
	}
	
	private static void filterMummerSnps(List<Range> ranges) {
		List<Integer> lst = new ArrayList<Integer>();
		Iterator<Range> itr = ranges.iterator();
		while(itr.hasNext())
		{
			Range r = itr.next();
			if (r.getLowerLimit() != r.getUpperLimit() || lst.contains(r.getLowerLimit()))
			{
				itr.remove();
			}
			else
			{
				lst.add(r.getLowerLimit());
			}
		}
	}

}
